package wiki.servlets;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import wiki.generic.utilities.FormatUnsupportedException;
import wiki.setup.Setup;

import com.oreilly.servlet.MultipartRequest;
/**
 * 
 * @author <p><strong>JwTeam</strong></p> 
 * Responsabile dell'upload sulle pagine
 */
public class UploadServlet extends HttpServlet{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession ses=req.getSession(false);
		if(ses!=null){
			boolean isDoc = (Boolean) ses.getAttribute("rights");
			if(isDoc){
		PrintWriter out=resp.getWriter();
		out.write("<html>\n" +
				"<head>\n" +
				"<title>Upload</title>\n" +
				"<link rel=\"stylesheet\" type=\"text/css\" href=\"./html/edit/upload.css\"/>\n" +
				"<script type=\"text/JavaScript\" src=\"./html/edit/upload.js\" ></script>\n" +
				"</head>\n" +
				"<body>\n" +
				"<div id=\"contents\">" +
				"<form action=\"./upload" );
		boolean d=false;
		if(req.getParameterMap().containsKey("down")){
			out.write("?down=down");
			d=true;
		}
		if(req.getParameterMap().containsKey("mod") && req.getParameterMap().containsKey("name") && req.getParameterMap().containsKey("type") &&
				req.getParameterMap().containsKey("ID")){
			if(d){
				out.write("&");
			} else{
				out.write("?");
			}
			out.write("mod=mod&name="+req.getParameter("name")+"&type="+req.getParameter("type")+"&ID="+req.getParameter("ID"));
		}
	
		out.write("\" method=\"post\" enctype= \"multipart/form-data\" name= \"form\" id= \"p-form\">\n" +
				"<table>\n" +
				"<tr>\n" +
				"<td class=\"col1\">Scegli il file: </td>\n" +
				"<td class=\"col2\">&nbsp;  </td>" +
				"</tr>" +
				"<tr>" +
				"<td class=\"col1\"> <input type= \"file\" name= \"file\" id= \"file\"></td>" +
				"<td class=\"col2\">&nbsp;</td>" +
				"</tr>\n" +
				"</table>\n" +
				"<img src=\"./html/immagini/ajax-loader.gif\" id=\"load-gif\">" +
				"<input type=\"submit\" name= \"Submit\"  id=\"bottone\" value= \"Carica\" onClick=\"loadVisible('load-gif');\">" +
				"</form>\n" +
				"</div>\n" +
				"</body>\n" +
				"</html>"); 
			} else{
				resp.sendRedirect("./html/errorpages/nopermessi.html");
				return;
			}
		} else{
				resp.sendRedirect("./html/LOGIN/login.html");
		}
		
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		HttpSession ses=req.getSession(false);
		if(ses!=null){
			boolean isDoc = (Boolean) ses.getAttribute("rights");
			if(isDoc){
		String corso=(String) ses.getAttribute("corso");
		
		String root="./webapps/wiki/sharing";
		
		MultipartRequest multi=new MultipartRequest(req, root, 16777216);
		
		File f=multi.getFile("file");
		String fileName=multi.getFilesystemName("file");
		fileName=fileName.replace("'", "_");
		fileName=fileName.replace("\"", "_");
		fileName=fileName.replace("&", "E");
		fileName=fileName.replace("$$||$$", "$$$$$$");
		try {
			String type=new String();
			if(!req.getParameterMap().containsKey("down")){
				type=Setup.getSetup().getMediator().getGpService().getFileType(fileName);
			} else {
				type="down";
			}
			String dest=root+"/"+type;
			while(Setup.getSetup().getMediator().getGpService().isDuplicate(dest, fileName)){
				fileName=Setup.getSetup().getMediator().getGpService().modifyName(fileName);
			}
			if(f!=null){
				File fOUT=new File(dest,fileName);
				FileInputStream fIS=new FileInputStream(f);
				FileOutputStream fOS=new FileOutputStream(fOUT);
				
				while(fIS.available()>0){
					fOS.write(fIS.read());
				}
				fIS.close();
				fOS.close();
			}
			f.delete();
			Setup.getSetup().getMediator().geteService().insertElement(corso,type, "./sharing/"+type+"/"+fileName);
			if(req.getParameterMap().containsKey("mod") && req.getParameterMap().containsKey("name") && req.getParameterMap().containsKey("type") &&
					req.getParameterMap().containsKey("ID")){
				System.out.println("jhgj");
				resp.sendRedirect("./mod?name="+req.getParameter("name")+"&type="+req.getParameter("type")+"&ID="+req.getParameter("ID"));
			} else {
			resp.sendRedirect("./"+type);	
			}
			
			
		} catch (FormatUnsupportedException e) {
			f.delete();
			resp.sendRedirect("./html/errorpages/nosupport.html");
		}
			} else{
				resp.sendRedirect("./html/errorpages/nopermessi.html");
				return;
			}
		} else{
			resp.sendRedirect("./html/LOGIN/login.html");
			return;
		}
	}
	
	
}
